﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Repository.Entities;
using Core.Extensions;

namespace Repository.Repositories
{
    public class TBTTCRepository : IRepository<TBTTC>
    {
        public IEnumerable<TBTTC> GetAll()
        {
            using (var context = new CNTTLAWEntities())
            {
                context.Connection.Open();
                context.TBTTCs.MergeOption = MergeOption.NoTracking;
                var d = context.TBTTCs;
                return context.TBTTCs.ToList();
            }
        }

        public IEnumerable<TBTTC> GetAll(string sortField)
        {
            using (var context = new CNTTLAWEntities())
            {
                context.TBTTCs.MergeOption = MergeOption.NoTracking;

                return context.TBTTCs.SortBy(sortField).ToList();
            }
        }

        public IEnumerable<TBTTC> GetSome(System.Linq.Expressions.Expression<Func<TBTTC, bool>> predicate)
        {
            using (var context = new CNTTLAWEntities())
            {
                context.TBTTCs.MergeOption = MergeOption.NoTracking;

                return context.TBTTCs.Where(predicate).ToList();
            }
        }

        public IEnumerable<TBTTC> GetSome(System.Linq.Expressions.Expression<Func<TBTTC, bool>> predicate, int top, int skip)
        {
            using (var context = new CNTTLAWEntities())
            {
                context.TBTTCs.MergeOption = MergeOption.NoTracking;

                return context.TBTTCs.Where(predicate).ToList();
            }
        }

        public TBTTC GetOne(System.Linq.Expressions.Expression<Func<TBTTC, bool>> predicate)
        {
            using (var context = new CNTTLAWEntities())
            {
                return context.TBTTCs.FirstOrDefault(predicate);
            }
        }

        public void Save(TBTTC original, TBTTC modified)
        {
            using (var context = new CNTTLAWEntities())
            {
                //modified.ModifiedDate = DateTime.UtcNow;

                if (original == null)
                    context.AddToTBTTCs(modified);
                else
                {
                    context.Attach(original);
                    context.ApplyCurrentValues("TBTTCs", modified);
                }

                context.SaveChanges();
            }
        }

        public void Delete(TBTTC original)
        {
            using (var context = new CNTTLAWEntities())
            {
                if (original.EntityKey == null)
                {
                    var existing = GetOne(d => d.MaTT == original.MaTT);
                    if (existing != null)
                    {
                        original.EntityKey = existing.EntityKey;
                    }
                }

                context.Attach(original);
                context.DeleteObject(original);

                context.SaveChanges();
            }
        }
    }
}
